//--------------------------------------------各类订单选择商品模块处理------------------------------------------------------------------------------
function genGoods(list) {
    let temp = "";
    $.each(list, function(i,goods){
        temp += goods.name + "、";
    });
    temp = temp.substring(0, temp.length-1);
    return temp;
}

function genPacketStock(packetVo) {
    let packetStock = undefined;
    for (var i=0;i<packetVo.packetGoodsVoList.length;i++) {
        let packetGoodsVo = packetVo.packetGoodsVoList[i];
        if (packetGoodsVo.baseStockNum==null) {
            packetStock=0;
            break;
        }

        let temp = parseInt(packetGoodsVo.baseStockNum/packetGoodsVo.number/packetGoodsVo.times);
        if (packetStock == undefined || packetStock > temp){
            packetStock = temp;
        }
    }
    return packetStock;
}


function genPacketStockWithUsed(packetVo, used_goods_number_map) {
    let packetStock = undefined;
    for (var i=0;i<packetVo.packetGoodsVoList.length;i++) {
        let packetGoodsVo = packetVo.packetGoodsVoList[i];
        if (packetGoodsVo.baseStockNum==null) {
            packetStock=0;
            break;
        }

        let usedNumber = used_goods_number_map[packetGoodsVo.goodsId] == undefined ? 0:used_goods_number_map[packetGoodsVo.goodsId] ;
        let remain = packetGoodsVo.baseStockNum - usedNumber;
        let temp = parseInt(remain/packetGoodsVo.number/packetGoodsVo.times);
        if (packetStock == undefined || packetStock > temp){
            packetStock = temp;
        }
    }
    return packetStock;
}

function genGoodsAndPacket(goodsList, packetList) {
    let temp = "";
    if (!emptyList(goodsList)) {
        $.each(goodsList, function(i,vo){
            temp += vo.goodsVo.name + "、";
        });
    }

    if (!emptyList(packetList)) {
        $.each(packetList, function (i, vo) {
            temp += vo.packetName + "、";
        });
    }
    if (!emptyEntity(temp)) {
        temp = temp.substring(0, temp.length-1);
    }
    return temp;
}

function genGoodsName(name,norms) {
    if (norms!=null && norms.length > 0) {
        name += "/"+norms;
    }
    return name;
}

function genGoodsNorm(norms) {
    if (norms!=null && norms.length > 0) {
        return norms;
    }
    return "";
}

function genAttr(attrNameVoList) {
    let attrStr = "";
    $.each(attrNameVoList, function(i,attrNameVo){
        $.each(attrNameVo.attrList, function(i,attr){
            attrStr += attrNameVo.name+ ":"+ attr.attrValue + ";";
        });
    });
    attrStr = attrStr.substring(0, attrStr.length-1);
    return attrStr;
}

function genUnitName(goodsUnitVoList,unitId) {
    let unitName;
    $.each(goodsUnitVoList, function (index, goodsUnitVo) {
        if (unitId == goodsUnitVo.unitId) {
            unitName = goodsUnitVo.unit.name;
            return false;
        }
    });
    return unitName;
}

function genUnitVo(goodsUnitVoList,unitId) {
    let vo;
    $.each(goodsUnitVoList, function (index, goodsUnitVo) {
        if (unitId == goodsUnitVo.unitId) {
            vo = goodsUnitVo;
            return false;
        }
    });
    return vo;
}

function genUnitVoByCode(goodsUnitVoList,code) {
    let vo;
    $.each(goodsUnitVoList, function (index, goodsUnitVo) {
        if (code == goodsUnitVo.code) {
            vo = goodsUnitVo;
            return false;
        }
    });
    return vo;
}

function genPriceName(goodsUnitVoList,unitId, priceFlag) {
    let goodsUnitVo;
    if (goodsUnitVoList.length == 1) {
        goodsUnitVo = goodsUnitVoList[0];
    } else {
        $.each(goodsUnitVoList, function (index, vo) {
            if (vo.unitId == unitId) {
                goodsUnitVo = vo;
                return false
            }
        });
    }

    if (priceFlag == 1) {
        return getPrice(goodsUnitVo.retailPrice) + "(零)";
    } else {
        return getPrice(goodsUnitVo.wholesalePrice) + "(批)";
    }
}

function genSelectUnit(unitId, goodsUnitVoList) {
    if (goodsUnitVoList.length == 1) {
        return "<input type='hidden' name='unitId' class='unitSelect' value='"+goodsUnitVoList[0].unitId+"'/>"+goodsUnitVoList[0].unit.name;
    }

    let child = "<select name='unitId' class='unitSelect' lay-filter='unitSelect'>";
    $.each(goodsUnitVoList, function(index, vo){
        if (index == 0) {
            child += "<option value='"+vo.unitId+"'>"+vo.unit.name+"(主)</option>";
        } else {
            if (unitId != null && vo.unitId == unitId){
                child += "<option selected='selected' value='"+vo.unitId+"'>"+vo.unit.name+"(="+vo.times + goodsUnitVoList[0].unit.name +")</option>";
            } else {
                child += "<option value='"+vo.unitId+"'>"+vo.unit.name+"(="+vo.times + goodsUnitVoList[0].unit.name +")</option>";
            }
        }
    });
    child += "</select>";
    return child;
}

function genPriceSelect(unitId, goodsUnitVoList, priceFlag) {
    let goodsUnitVo;
    if (goodsUnitVoList.length == 1) {
        goodsUnitVo = goodsUnitVoList[0];
    } else {
        $.each(goodsUnitVoList, function(index, vo){
            if (vo.unitId == unitId) {
                goodsUnitVo = vo;
            }
        });
    }

    let child = "<select class='price' lay-filter='price'>";

    child += "<option value='1-"+goodsUnitVo.retailPrice+"'>"+ getPrice(goodsUnitVo.retailPrice) +"(零)</option>";
    if (priceFlag == 2){
        child += "<option selected='selected' value='2-"+goodsUnitVo.wholesalePrice+"'>"+ getPrice(goodsUnitVo.wholesalePrice) +"(批)</option>";
    } else {
        child += "<option value='2-"+goodsUnitVo.wholesalePrice+"'>"+ getPrice(goodsUnitVo.wholesalePrice) +"(批)</option>";
    }
    child += "</select>";
    return child;
}

/**
 *
 * @param data
 * @param type 1-表示加 2-表示减最小减到0 3-表示减最小减到1， 未定义表示不处理
 * @returns {number}
 */
function genNumber(data, type) {
    let number;
    if (!isNumber(data)){
        number = 0;
    } else {
        number = getNumber(data);
    }

    if(number < 0) {
        number = -number;
    }

    if (type == 1){
        number++;
    }
    if (type==2 && number > 0){
        number--;
    }
    if (type==3 && number > 1){
        number--;
    }
    return number;
}

function genAmount(data) {
    let amount;
    if (!isNumber(data)){
        amount = 0;
    } else {
        amount = parseFloat(data);
    }

    if(amount < 0) {
        amount = -amount;
    }
    return amount;
}

function genDiscountRate(data) {
    let rate;
    if (!isNumber(data)){
        rate = 100;
    } else {
        rate = parseFloat(data);
    }

    if(rate < 0) {
        rate = -rate;
    }

    if (rate > 100){
        rate = 100;
        tip("折扣不能大于100");
    }
    return rate;
}

function setTotalAmount() {
    let tableData = layui.table.cache.table;
    let totalAmount = 0;
    $.each(tableData, function (i,data) {
        totalAmount += parseFloat(data.amount);
    });

    let discountFlag = $("#totalAmount").attr("discountFlag");
    if (discountFlag != undefined){
        if (discountFlag == 1) {
            totalAmount = totalAmount * parseFloat($("#discountRate").val())/100;
        } else {
            totalAmount -= parseFloat($("#discountAmt").val());
        }
    }

    let totalWithOutOtherAmt = totalAmount;

    let otherAmt = $("#otherAmt").val();
    if (otherAmt!=undefined){
        totalAmount += parseFloat(otherAmt);
    }


    let href = window.location.href;
    if (href.indexOf("inGoodsNew") != -1 || href.indexOf("inGoodsRefundNew") != -1){

        if (_businessSet.inTaxRateFlag){
            //开启税率
            let tax = totalWithOutOtherAmt * parseFloat(_businessSet.inTaxRate)/100;
            $("#tax").html(getPrice(tax));
            totalAmount = totalAmount+tax;
        }


        totalAmount = getPrice(totalAmount);
        $("#totalAmount").val(totalAmount);
        $("#totalAmount").html(totalAmount);
        if (_businessSet.inMakeUp){
            console.log("进货并开启了自动补齐实付金额");
            $("#realAmt").val(totalAmount);
            $("#realAmt").html(totalAmount);
        }
    } else if (href.indexOf("saleNew") != -1 || href.indexOf("refundNew") != -1) {
        if (_businessSet.saleTaxRateFlag){
            //开启税率
            let tax = totalWithOutOtherAmt * parseFloat(_businessSet.saleTaxRate)/100;
            $("#tax").html(getPrice(tax));
            totalAmount = totalAmount+tax;
        }

        totalAmount = getPrice(totalAmount);
        $("#totalAmount").val(totalAmount);
        $("#totalAmount").html(totalAmount);
        if (_businessSet.saleMakeUp){
            console.log("销售并开启了自动补齐实付金额");
            $("#realAmt").val(totalAmount);
        }
    }else{
        totalAmount = getPrice(totalAmount);
        $("#totalAmount").val(totalAmount);
        $("#totalAmount").html(totalAmount);
        $("#realAmt").html(totalAmount);
        $("#realAmt").val(totalAmount);
    }

}

/**
 * 设置金额
 * @param amount
 * @param $tdChild
 */
function setAmount(amount, $tdChild) {
    layui.use(['form', 'table'], function () {
        let $ = layui.jquery,
            table = layui.table;
        let index = $tdChild.parents('tr').data('index');
        let tableData = layui.table.cache.table;
        tableData[index].amount = amount;
        console.log("amount："+ tableData[index].amount);
        $tdChild.parents('tr').find(".numberDiv .amount").val(getPrice(tableData[index].amount));

        if (tableData[index].amount!=0 && tableData[index].number!=0){
            tableData[index].price = getPrice(tableData[index].amount/tableData[index].number);
            $tdChild.parents('tr').find(".priceDiv .price").val(tableData[index].price);
        }
        setTotalAmount();
    });
}

//处理金额显示
function processAmount($tdChild){
    let index = $tdChild.parents('tr').data('index');
    let tableData = layui.table.cache.table;
    let number = tableData[index].number;
    if (!isNumber(tableData[index].price)) {
        tableData[index].price = "0";
    }

    let price = parseFloat(tableData[index].price);
    let amount = price * number;
    amount = getPrice(amount);
    setAmount(amount, $tdChild);
}

//处理数量空间
function processNumber(number, $input, noAmt,numberZero) {
    if (number == 0 && (numberZero==undefined || !numberZero)) {
        $input.val("");
        $input.html("");
    } else {
        $input.val(number);
        $input.html(number);
    }

    let index = $input.parents('tr').data('index');
    let tableData = layui.table.cache.table;
    tableData[index].number = number;

    if (tableData[index].packetId == undefined || tableData[index].packetId == null){//不是套餐
        //不是整数，但是单位有不支持小数
        let goodsUnitVo = genUnitVo(tableData[index].goodsUnitVoList, tableData[index].unitId);

        if (!isInteger(number) && ((goodsUnitVo.unit.point!=null && !goodsUnitVo.unit.point))){
            errTip("此单位不允许小数，可以在单位中设置");
            $input.val("");
            $input.html("");
            tableData[index].number = 0;
            return;
        }
    } else {
        //是套餐
        if (!isInteger(number)){
            errTip("套餐不允许小数");
            $input.val("");
            $input.html("");
            tableData[index].number = 0;
            return;
        }
    }


    if (!noAmt){
        processAmount($input);
    }
}




function reloadGoodsTable() {
    let tableData = layui.table.cache.table;
    layui.table.reload('table',{
        data:tableData
    });
}

function operator_del(obj) {
    let index = $(obj.tr).data('index');
    layui.table.cache.table.splice(index,1);
    reloadGoodsTable();
}

function genGoodsAndPacketNumber(goodsList, packetList) {
    let number = 0;
    $.each(goodsList, function(i,vo){
        number += vo.number;
    });


    $.each(packetList, function(i,vo){
        number += vo.number;
    });
    return number;
}

function genGoodsNumber(list) {
    let number = 0;
    $.each(list, function(i,vo){
        number += vo.number;
    });
    return number;
}

function genDisCountAmt(totalAmt, discountRate,discountAmt) {
    if(isNumber(discountRate)){
        totalAmt = parseFloat(totalAmt);
        discountRate = parseFloat(discountRate) / 100;
        let amt = totalAmt / discountRate;
        amt = amt-totalAmt;
        return getPrice(amt);
    } else {
        return getPrice(discountAmt);
    }
}

/**
 * 检查是否包含id
 * @param list
 * @param id
 */
function hasId(list,id) {
    let flag = false;
    $.each(list, function (index, vo) {
        if (vo.id == id) {
            flag = true;
            return false;
        }
    });
    return flag;
}